package ro.gateway.aida.sectors;

import java.sql.SQLException;
import java.util.Hashtable;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import ro.gateway.aida.db.PersistenceManager;
import ro.gateway.aida.db.PersistenceToken;
import ro.gateway.aida.obj.AIDASector;
import ro.gateway.aida.obj.db.AIDASectorPersMan;
import ro.gateway.aida.servlet.Constants;
import ro.gateway.aida.utils.HttpUtils;
import ro.xblue.translator.LanguageBean;
import ro.xblue.translator.TranslatorUtils;



/**
 * <p>Title: Romanian AIDA</p>
 * <p>Description: </p>
 * <p>Copyright: Copyright (comparator) 2003</p>
 * <p>Company: ro-gateway</p>
 * @author Mihai Popoaei, mihai_popoaei@yahoo.com, smike@intellisource.ro
 * @version 1.0-* @version $Id: BrowserAction.java,v 1.1 2004/10/24 23:37:09 mihaipostelnicu Exp $
 */
public class BrowserAction extends Action
{
      public ActionForward execute(
            ActionMapping mapping,
            ActionForm form,
            HttpServletRequest request,
            HttpServletResponse response )
            throws Exception
      {
            ServletContext application = this.getServlet().getServletContext();
            PersistenceToken pers_token = PersistenceManager.
                  tokenLookup( application );

            HttpSession session = request.getSession();
            Hashtable services = ( Hashtable ) application.
                  getAttribute( Constants.SERVICES );


            SectorChooserForm myform = ( SectorChooserForm ) form;
            myform.action = HttpUtils.getValidTrimedString(
                  request, "action", null );

            LanguageBean lang_bean = TranslatorUtils.
                  getCurrentLanguage( request, application );

            if ( "save".equals( myform.action ) )
            {
                  if ( myform.sector == null ) myform.action = "cancel";
                  return mapping.findForward( "finnish" );
            }
            else if ( "cancel".equals( myform.action ) )
            {
                  myform.clear_objects();
                  return mapping.findForward( "finnish" );
            }

            myform.clear_objects();
            myform.setSector_id( HttpUtils.getInt( request, "sector_id", -1 ) );
            myform.setSubsector_id( HttpUtils.getInt( request, "subsector_id", -1 ) );
            myform.setSubsector1_id( HttpUtils.getInt( request, "subsector1_id", -1 ) );
            try
            {
                  AIDASector[] _sectors = AIDASectorPersMan.
                        getManager( pers_token ).
                        getAllRoots();
                  SectorWrapper[] sectors = SectorWrapper.
                        wrapp( _sectors, lang_bean.language );
                  if ( sectors != null )
                  {
                        request.setAttribute( "sectors", sectors );
                  }
                  else
                  {
                        System.err.println( "NO SECTORS!" );
                  }

                  if ( myform.sector_id != -1 )
                  {
                        myform.sector = AIDASectorPersMan.
                              getManager( pers_token ).getById( myform.sector_id );
                  }

                  if ( myform.sector != null )
                  {
                        AIDASector[] _subsectors = AIDASectorPersMan.
                              getManager( pers_token ).getAllForParent(
                                    myform.sector.getRo_aida_code() );

                        SectorWrapper[] subsectors = SectorWrapper.
                              wrapp( _subsectors, lang_bean.language );
                        if ( subsectors != null )
                        {
                              request.setAttribute( "subsectors", subsectors );
                        }

                        if ( myform.subsector_id != -1 )
                        {
                              myform.subsector = AIDASectorPersMan.
                                    getManager( pers_token ).getById( myform.subsector_id );
                              if ( myform.subsector != null )
                              {
                                    AIDASector[] _subsectors1 = AIDASectorPersMan.
                                          getManager( pers_token ).getAllForParent(
                                                myform.subsector.getRo_aida_code()
                                          );
                                    if ( _subsectors1 != null )
                                    {
                                          SectorWrapper[] subsectors1 = SectorWrapper.
                                                wrapp( _subsectors1, lang_bean.language );
                                          request.setAttribute( "subsectors1", subsectors1 );
                                    }
                              }
                        }
                  }
            }
            catch ( SQLException sqlEx )
            {
                  sqlEx.printStackTrace();
            }

            request.setAttribute( "marked_field", myform.getMarkedField() );
            return mapping.findForward( "browser_jsp" );
      }
}
